#include <func.h>
#include <mysql/mysql.h>

//对输入的命令路径进行切片：cd dir --> dir
char* pathSlice(const char* path) {
    char* result = strrchr(path, ' ');
    if (result) {
        result++; // 跳过空格，指向第一个字符
    }
    return result; // 返回最后一级目录的指针
}

//简化路径：dir/../dir2 --> dir2
char* simplifyPath(char* path){
    unsigned int len = strlen(path);
    char *ret = (char *)malloc(sizeof(char) * (len + 1));
    memset(ret, 0, sizeof(char) * (len + 1));
    int top = 0;
    ret[top++] = '/';
    for(int i=0; i<len; i++){
        if(path[i] == '/' && ret[top - 1] == '/'){
            continue;
        }
        if(path[i] == '.'){
            int cnt = 0;
            while(path[i] == '.'){
                cnt++;
                i++;
            }
            i--;
            if(cnt > 2){
                for(int j=0; j<cnt; j++){
                    ret[top++] = '.';
                }
            }else if(cnt == 2){
                if((i+1 < len && path[i+1] != '/') || (i > 1 &&path[i-2] != '/')){
                    ret[top++] = '.';
                    ret[top++] = '.';
                }else if(top > 1){
                    top--;
                    while(ret[top -1] != '/'){
                        top--;
                    }
                }
            }else if((cnt == 1 && i+1 < len && path[i+1] != '/')|| path[i-1] != '/'){
                ret[top++] = '.';
            }
            continue;
        }
        ret[top++] = path[i];
    }
    while(ret[top - 1] == '/' && top > 1){
        top--;
    }
    ret[top] = '\0';
    return ret;
}

int myCd(char* cmd, char* dirPath, char* username, int dirId){
    int flag1,flag2,flag3;  //用来判断用户名和路径是否都存在于数据库中
    char connect[512];
    char* afterSliceCmd = pathSlice(cmd); //dir1/dir2/../dir3
    strcat(dirPath,afterSliceCmd);
    strcat(dirPath,"/");
    strcpy(connect,simplifyPath(dirPath)); //connect获取到可以在数据库中查询的路径:dir1/dir3
    //根据connect存储的路径和用户名在数据库中查询该路径是否存在
    MYSQL * mysql = mysql_init(NULL);
    MYSQL * ret = mysql_real_connect(
                                     mysql,
                                     "localhost",
                                     "root",
                                     "123123",
                                     "netdisk",
                                     0,NULL,0
                                    );
    if(ret == NULL){
        fprintf(stderr, "mysql_real_connect:%s\n", mysql_error(mysql));
        return -1;
    }

    // fileinfo table
    char sql[1024] = "select * from fileinfo;";
    int qret = mysql_query(mysql, sql);
    if(qret != 0){
        fprintf(stderr, "mysql_query:%s\n", mysql_error(mysql));
        return -1;
    }

    MYSQL_RES *res = mysql_store_result(mysql);

    while(1){
        MYSQL_ROW row = mysql_fetch_row(res);
        if(row == NULL){
            break;
        }
        for(unsigned j = 0; j < mysql_num_fields(res); j++){
            if(strcmp(row[j],connect) == 0){
                flag1 = 1;
            }
            if(strcmp(row[j],username) == 0){
                flag2 = 1;
            }
            if(strcmp(row[j],"d")){
                flag3 = 1;
            }
        }
        if(flag1 == 1 && flag2 == 1 && flag3 == 1){
            dirId =atoi(row[0]);
            printf("%d\n",dirId);
            break;
        }
        flag1 = 0;
        flag2 = 0;
        flag3 = 0;
    }
    mysql_free_result(res);
    mysql_close(mysql);
    return dirId;
}

int main(int argc,char* argv[])
{
    char cmd[]="cd dir1";
    char dirPath[128] = "/";
    char username[] = "zhangsan";
    int dirId = 0;
    int res ;
    res =  myCd(cmd,dirPath,username,dirId);
    printf("res = %d\n",res);
    return 0;
}

